package me.sdau.spider.openapi.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.sdau.spider.openapi.comm.utils.MD5Util;
import me.sdau.spider.openapi.entity.User;
import me.sdau.spider.openapi.repository.UserRepository;
import me.sdau.spider.openapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public void Create(String account, String appkey) {
        String salt = UUID.randomUUID().toString();
        User user = User.builder()
                .account(account)
                .appkey(MD5Util.encrypt(appkey, salt))
                .salt(salt)
                .build();
        user = userRepository.save(user);
        log.debug("用户信息 ==> [{}]", user);
    }

    @Override
    public User getUserByAccount(String account) {
        return userRepository.findByAccount(account, new Date());
    }
}
